package com.wunong.smart.commission.domain.state.operation;

import com.wunong.smart.commission.client.enums.CommissionOperationEnum;
import com.wunong.smart.domain.component.register.BaseSingletonInitBean;
import org.apache.commons.collections.MapUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 操作工厂类
 *
 * @author create by zealot.zt
 */
@Component(CommissionOperationFactory.BEAN_NAME)
public class CommissionOperationFactory extends BaseSingletonInitBean {

    public static final String BEAN_NAME = "com.wunong.smart.commission.domain.state.operation.CommissionOperationFactory";

    private static Map<CommissionOperationEnum, CommissionOperation> operationMap = new HashMap<>();

    @Override
    protected void onInit(ApplicationContext context) {
        Map<String, CommissionOperation> beans = applicationContext.getBeansOfType(CommissionOperation.class);
        if (MapUtils.isNotEmpty(beans)) {
            beans.forEach((k, v) -> {
                if (operationMap.containsKey(v.getOperationEnum())) {
                    throw new IllegalArgumentException("亲，存在相同的拼团活动操作：" + v.getOperationEnum());
                }

                operationMap.put(v.getOperationEnum(), v);
            });
        }
    }

    /**
     * 根据操作枚举获取对应的操作类
     *
     * @param operationEnum
     * @return
     */
    public static CommissionOperation getOperation(CommissionOperationEnum operationEnum) {
        return operationMap.get(operationEnum);
    }

}
